/**
 * form-item 默认配置
 */
const dateObj = {
    props: { checkStrictly: true },
    clearable: true,
    placeholder: '请选择',
    // format: 'timestamp',
    'value-format': 'timestamp'
}

export const ItemDefCof = {
    // input 默认项
    input: {
        maxlength: '500',
        placeholder: '请输入',
        clearable: true
    },

    // textarea 默认项
    textarea: {
        maxlength: '200',
        rows: 2,
        autosize: {
            minRows: 2
        },
        'show-word-limit': true,
        placeholder: '请输入',
        clearable: true
    },

    // number 默认项
    number: {
        min: 0,
        precision: 2,
        step: 0.1,
        // 'controls-position': 'right',
        controls: true,
        placeholder: '请输入'
    },

    // select 默认项
    select: {
        placeholder: '请选择',
        clearable: true
    },

    // cascader 默认项
    cascader: {
        props: { checkStrictly: true },
        clearable: true,
        placeholder: '请选择'
    },

    // date month year daterange datetime datetimerange monthrange
    date: dateObj,
    month: dateObj,
    year: dateObj,
    daterange: {
        ...dateObj,
        'start-placeholder': '开始日期',
        'end-placeholder': '结束日期'
    },
    time: dateObj,
    datetime: {
        ...dateObj,
        'start-placeholder': '开始时间',
        'end-placeholder': '结束时间'
    },
    datetimerange: {
        ...dateObj,
        'start-placeholder': '开始时间',
        'end-placeholder': '结束时间'
    },
    monthrange: {
        ...dateObj,
        'start-placeholder': '开始月份',
        'end-placeholder': '结束月份'
    },

    // switch 默认项
    switch: {
        activeText: '是',
        inactiveText: '否',
        placeholder: '请选择',
    },

    // codeTable 默认项
    codeTable: {
        clearable: true,
        placeholder: '请选择',
        filterable: true,
        options: [
            { label: '选项1', value: '1' },
            { label: '选项2', value: '2' },
        ]
    },
    //树形
    tree: {
        clearable: true,
        placeholder: '请选择'
    },
    //自动补全
    autocomplete: {
        clearable: true,
        placeholder: '请选择',
        filterable: true
    }
}

// DatePicker类型,含 DateTimePicker
export const dateType = [
    'year',
    'month',
    'date',
    'dates',
    'datetime',
    'week',
    'datetimerange',
    'daterange',
    'monthrange'
]

// input原生类型，保留 password,tel,email,color,url
export const orginInput = [
    'input',
    'password',
    'tel',
    'email',
    'color',
    'url'
]

/**
 * @description:根据字符串路径获取对象属性
 * @return {*}
 * @author: zhz
 */
export function getPropByPath(obj, path, strict) {
    let tempObj = obj
    path = path.replace(/\[(\w+)\]/g, '.$1') // 将[0]转化为.0
    path = path.replace(/^\./, '') // 去除开头的.
    const keyArr = path.split('.') // 根据.切割
    let i = 0
    for (let len = keyArr.length; i < len - 1; ++i) {
        if (!tempObj && !strict) break
        const key = keyArr[i]
        if (key in tempObj) {
            tempObj = tempObj[key]
        } else {
            if (strict) {
                // 开启严格模式，没找到对应key值，抛出错误
                throw new Error('please transfer a valid prop path to form item!')
            }
            break
        }
    }
    return {
        o: tempObj, // 原始数据
        k: keyArr[i], // key值
        v: tempObj ? tempObj[keyArr[i]] : null // key值对应的值
    }
}
